package org.i_plus.model.services;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.i_plus.model.BlogBean;
import org.i_plus.model.BlogDAO;
import org.i_plus.model.MemberBean;
import org.i_plus.model.dao.MemberDAOHibernate;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class BlogService {
	private BlogDAO dao;
	private MemberDAOHibernate mdao ;
	public BlogService(BlogDAO dao){
		this.dao=dao;
	}
	public void setMemberDAO(MemberDAOHibernate mdao){
		this.mdao=mdao;
	}
	
	public BlogBean selectB(BlogBean bean){
		BlogBean result = null;
			if(bean!=null && bean.getB_num()!=0){
				result=dao.selectByBbum(bean.getB_num());
//				System.out.println(temp.getMemberBean());
				//MemberDAOHibernate mdao = new MemberDAOHibernate();   spring 不能new
				MemberBean mbean=mdao.Select_By_Account(result.getMemberBean().getAccount());
//				System.out.println(temp.getMemberBean().getAccount());
//				mbean.setPic1(null);
//				mbean.setPic2(null);
				result.setMemberBean(mbean);
//				System.out.println(temp.getMemberBean().getName());
//				System.out.println(temp.getB_time());
//				System.out.println(temp);
				// 抓關聯資料庫的名字
//				String temp2=temp.getMemberBean().getAccount();
//				MemberDAOHibernate dao = new MemberDAOHibernate();
//				temp.setMemberBean(dao.Select_By_Account(temp2));
//				String name=temp.getMemberBean().getName();
				
			}
		return result;
	}
	public List<BlogBean> selectbyAcc(String account){
		List<BlogBean> result=dao.selectbyAcc(account);
		return result;
	}
	public BlogBean insertB(BlogBean bean){
		BlogBean result=null;
		Date date = new Date();
		Timestamp timestamp = new Timestamp(date.getTime());
		bean.setB_time(timestamp);
		if(bean!=null){
			dao.insertB(bean.getMemberBean().getAccount(), bean.getContent(), bean.getB_time());
			result=bean;
		}
		return result;
	}
	public BlogBean updateB(BlogBean bean){
		BlogBean result=null;
		Date date = new Date();
		Timestamp timestamp = new Timestamp(date.getTime());
		if(bean!=null){
			bean.setB_time(timestamp);
			dao.updateB(bean.getMemberBean().getAccount(), bean.getB_num(), bean.getContent(), bean.getB_time());
//			System.out.println("bean.getMemberBean().getAccount():"+bean.getMemberBean().getAccount());
//			System.out.println("bean.getB_num():"+bean.getB_num());
			result=bean;
		}
		return result;
	}
	public boolean deleteB(BlogBean bean){
		boolean result=false;
			if(bean!=null){
			 dao.deleteB(bean.getB_num());
			 	if(dao.selectByBbum(bean.getB_num())==null){
			 		result=true;
			 	}
			}
		return result;
	}
	public static void main(String[] args){
		ApplicationContext context =
				new ClassPathXmlApplicationContext("beans.config.xml");
		SessionFactory sessionFactory = (SessionFactory) context.getBean("sessionFactory");
		Session session=sessionFactory.getCurrentSession();
		session.beginTransaction();
		
		BlogService service = (BlogService) context.getBean("blogService");
		
		
//		int b_num=18;
//		BlogBean bean=(BlogBean) session.get(BlogBean.class,b_num);
//		service.selectB(bean);
		
//		bean.setContent("rrrr");
//		service.updateB(bean);
		session.getTransaction().commit();
//		List<BlogBean> beans=service.selectB(null);
//		for(BlogBean bean:beans){
//			System.out.println(bean);
//		}
	}
}
